package com.heima.wemedia.controller.v1;

import com.heima.common.constants.media.WeMediaConstants;
import com.heima.common.exception.LeadNewsException;
import com.heima.model.common.dto.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dto.WmMaterialDto;
import com.heima.wemedia.service.MaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.Objects;

/**
 * @author Ronaldo
 * @date 2021/8/3
 */
@RestController
@RequestMapping("/api/v1/material")
@Slf4j
public class MaterialController {

    @Autowired
    private MaterialService materialService;

    @PostMapping("/upload_picture")
    public ResponseResult uploadPicture(MultipartFile multipartFile){
        log.info("upload picture by {}" + multipartFile);

        if (Objects.isNull(multipartFile) || multipartFile.isEmpty()){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        return materialService.uploadPicture(multipartFile);
    }

    @PostMapping("/list")
    public ResponseResult queryMaterialList(@RequestBody WmMaterialDto wmMaterialDto){
        log.info(" page query material list {}" + wmMaterialDto);

        if (Objects.isNull(wmMaterialDto)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        return materialService.queryMaterialList(wmMaterialDto);
    }

    @GetMapping("/del_picture/{id}")
    public ResponseResult deleteMaterialById(@PathVariable Integer id){
        log.info("delete material by id {}" + id);

        if (Objects.isNull(id)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        return materialService.deleteMaterialById(id);
    }

    @GetMapping("/collect/{id}")
    public ResponseResult collect(@PathVariable("id") Integer id){
        log.info("update material status by id {}" + id);

        if (Objects.isNull(id)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        return materialService.collect(id, WeMediaConstants.COLLECT_MATERIAL);
    }


    @GetMapping("/cancel_collect/{id}")
    public ResponseResult cancelCollect(@PathVariable("id") Integer id){
        log.info("update material status by id {}" + id);

        if (Objects.isNull(id)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        return materialService.collect(id, WeMediaConstants.CANCEL_COLLECT_MATERIAL);
    }
}
